package com.sp.fresh_produce.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;

/**
 * 二维码生成器
 */
public class QRCodeGenerator {

    public static void generateQRCode(String text, int width, int height, String filePath) throws WriterException, IOException {
        // 生成二维码
        QRCodeWriter codeWriter = new QRCodeWriter();
        // 创建二维码
        BitMatrix bitMatrix = codeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        // 创建文件路径
        Path path = FileSystems.getDefault().getPath(filePath);
        // 这里可以添加将BitMatrix转换为图片并保存到filePath的代码
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    }
}
